
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 a
 * @author Abahe
 */
public class AskSoundScreen extends GameCanvas {

    MMIDlet midlet;
    //private Image imgAskSound;
    private Image imgSoundOff;
    private Image imgSoundOn;
    private Image imgMuteOff;
    private Image imgMuteOn;
    private Image imgBackground;
    private int status = 1; //1 yes, 2 no
    private int xBg = 0;
    private int yBg = 0;

    private Sprite spKursor;
    private Sprite spSound;
    private Sprite spMute;
    private String pathKursor;
    private LayerManager lmgr;

    private int xSound = 0;
    private int ySound = 0;
    private int xMute = 0;
    private int yMute = 0;

    public AskSoundScreen(MMIDlet midlet) {
        super(false);
        setFullScreenMode(true);
        this.midlet = midlet;

        if (MMIDlet.LANDSCAPE == 1) {
            yBg = - 41;
            pathKursor = "/set_main_menu_cursor_36x240.png";
            //set_main_menu_cursor_36x240
        } else {
            xBg = - 39;
            pathKursor = "/set_main_menu_cursor_36x320.png";
        }
        
        try {
            //imgAskSound = Image.createImage("/ask_sound.png");
            //imgAskSound = Image.createImage("/yoho2.png");
            spKursor = new Sprite(Image.createImage(pathKursor));
            
            imgSoundOff = Image.createImage("/set_icon_soundon_36x25.png");
            imgSoundOn = Image.createImage("/set_icon_soundon_36x25.png");
            imgMuteOff = Image.createImage("/set_icon_sound Off_36x25.png");
            imgMuteOn = Image.createImage("/set_icon_sound Off_36x25.png");
            imgBackground = Image.createImage("/set_mainmenubg_all_320x320.png");

            spSound = new Sprite(Image.createImage(imgSoundOn));
            spMute = new Sprite(Image.createImage(imgMuteOn));
        } catch (IOException e) {
        }

        lmgr = new LayerManager();
        lmgr.append(spSound);
        lmgr.append(spMute);
        lmgr.append(spKursor);

        xSound = (getWidth()/2) - imgSoundOn.getWidth() - 40;
        ySound = (getHeight()/2) - (imgSoundOn.getHeight()/2);

        xMute = (getWidth()/2) + imgMuteOn.getWidth();
        yMute = (getHeight()/2) - (imgMuteOn.getHeight()/2);

        spKursor.setPosition(xSound, 0);
        spSound.setPosition(xSound, ySound);
        spMute.setPosition(xMute, yMute);
        drawScreen();
        
    }

    private void drawScreen() {

        Graphics g = this.getGraphics();
        //g.setColor(0x000077);
        g.drawImage(imgBackground, xBg, yBg, Graphics.TOP | Graphics.LEFT);

        //g.drawImage(imgBackground, 0, 0, Graphics.TOP | Graphics.LEFT);


        if (status == 1) {
            /*
            g.drawImage(imgSoundOn, getWidth()/2 - 50,
                    getHeight()/2 - (imgSoundOn.getHeight()/2),
                    Graphics.TOP | Graphics.LEFT);
            g.drawImage(imgMuteOff, getWidth()/2 + 50,
                    getHeight()/2 - (imgSoundOn.getHeight()/2),
                    Graphics.TOP | Graphics.LEFT);
             * 
             */
            spKursor.setPosition(xSound, 0);
        } else if (status == 2) {
            /*
            g.drawImage(imgSoundOn, getWidth()/2 - 50,
                    getHeight()/2 - (imgSoundOn.getHeight()/2),
                    Graphics.TOP | Graphics.LEFT);
            g.drawImage(imgMuteOff, getWidth()/2 + 50,
                    getHeight()/2 - (imgSoundOn.getHeight()/2),
                    Graphics.TOP | Graphics.LEFT);
             * 
             */
            spKursor.setPosition(xMute, 0);
        }

        lmgr.paint(g, 0, 0);
        flushGraphics();
    }

    protected void keyPressed(int keyCode) {

        String statusKey = String.valueOf(keyCode);
        System.out.println("key : " + statusKey);

        //portrait
        if (keyCode == -5) {
            //midlet.exit();
            if (status == 1) {
                midlet.showTitleScreen();
                MMIDlet.SOUND = 1;
            } else {
                System.out.println("no");
                midlet.showTitleScreen();
                MMIDlet.SOUND = 0;
            }
        }

        if (keyCode == 53) { //5 (portrait no fire)
            //midlet.exit();
            if (status == 1) {
                midlet.showTitleScreen();
            } else {
                System.out.println("no");
            }
        }

        if (keyCode == 103) { //5 (qwerty no fire)
            //midlet.exit();
            if (status == 1) {
                midlet.showTitleScreen();
            } else {
                System.out.println("no");
            }
        }

        if (keyCode == -4) { //left
            if (status == 1) {
                status = 2;
            } else {
                status = 1;
            }
        }
        if (keyCode == -3) { //right
            if (status == 1) {
                status = 2;
            } else {
                status = 1;
            }
        }

        repaint();
        drawScreen();
    }
}
